﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;

namespace MyAlbum.Utility
{
    public static class PostExtensions
    {
        public static string GetUrl(this BlogML.Xml.BlogMLPost post, RequestContext context, RouteCollection routes)
        {
            return routes.GetUrl(context, "Post", new { id = post.ID });
        }

        public static string GetUrl(this RouteCollection routes, RequestContext context, string route, object values)
        {
            VirtualPathData pathData = routes[route].GetVirtualPath(context, new RouteValueDictionary(values));
            string path = "";

            if (pathData != null)
            {
                path = pathData.VirtualPath;

                if (!path.StartsWith("/"))
                    path = "/" + path;

                string appPath = context.HttpContext.Request.ApplicationPath;

                if (!path.StartsWith(appPath))
                    path = appPath + path;
            }

            return path;
        }
    }
}
